package com.clip.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.clip.entity.User;
import com.clip.entity.virtual.EventForView;

@Service
@Transactional
public class ClipEventService {
//	@Autowired
//	private DateEventDAO dateEventDAO;
//	@Autowired
//	private MonthRepeatDAO monthRepeatDAO;
//	@Autowired
//	private YearRepeatDAO yearRepeatDAO;
	
	private SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
	private String date2String(long datetime){
		if(datetime>0)
			return "提醒时间："+dateFormat.format(new Date(datetime));
		else
			return "";
	}
	public List<EventForView> getTodayEvent(Calendar calendar, User currentUser) {
		List<EventForView> events=getEventsByDay((Calendar)calendar.clone(),calendar,currentUser,true);
		return events;
	}
	
	public List<EventForView> getEventsByDay(Calendar calendar,
			Calendar now, User currentUser, boolean isThisMonth) {
		Calendar today=(Calendar) calendar.clone();
		today.setTime(calendar.getTime());
		today.clear(Calendar.MILLISECOND);
		today.clear(Calendar.SECOND);
		today.clear(Calendar.MINUTE);
		today.set(Calendar.HOUR_OF_DAY, 0);
		long todaybegin=today.getTimeInMillis();
		today.add(Calendar.DAY_OF_MONTH, 1);
		long todayend=today.getTimeInMillis();
//		String query1="from DateEvent date where date.uid="+currentUser.getId()+" and date.repeatid='0' and " +
//				"date.datetime>="+todaybegin+" and date.datetime<"+todayend;
//		//读取开始时间或者结束时间在今天内的事件，包括重复和非重复事件
//		List<DateEvent> dates1=dateEventDAO.findByQuery(query1);
//		String query2="from DateEvent date where date.uid="+currentUser.getId()+" and date.repeatid<>'0'";
//		List<DateEvent> dates2=dateEventDAO.findByQuery(query2);
//		List<EventForView> events=filterEventForToday(dates2,todaybegin,todayend,calendar,isThisMonth);
//		for (DateEvent dateEvent : dates1) {
//			long clocktime=dateEvent.getAlertstatus();
//			Calendar calStart=Calendar.getInstance();
//			calStart.setTimeInMillis(dateEvent.getDatetime());
//			EventForView eventForView=new EventForView();
//			eventForView.setThisMonth(isThisMonth);
//			eventForView.setEventid("date_"+dateEvent.getId());
//			eventForView.setAddress(dateEvent.getAddress());
//			eventForView.setContent(dateEvent.getContent());
//			eventForView.setTitle(dateEvent.getTitle());
//			eventForView.setAllDay(true);
//			eventForView.setStarthour(-1);
//			eventForView.setEndhour(-1);
//			eventForView.setEventtype(dateEvent.getEventtype());
//			eventForView.setStarttime(calStart.getTimeInMillis());
//			eventForView.setEndtime(calStart.getTimeInMillis());
//			if(clocktime==0l){
//				eventForView.setClocked(true);
//			}else{
//				
//				if(clocktime<now.getTimeInMillis()){
//					eventForView.setClocked(true);
//				}else
//					eventForView.setClocked(false);
//			}
//			eventForView.setClocktime(date2String(clocktime));
//			eventForView.setRepeatdesc(null);
//			events.add(eventForView);
//		}
		return null;
	}
	public Map getEventsByMonth(int year, int month,
			Calendar now, User currentUser) {
		Map maps=new HashMap<String, List>();
		Calendar firstDateOfMonth=Calendar.getInstance();
		firstDateOfMonth.clear(Calendar.MILLISECOND);
		firstDateOfMonth.clear(Calendar.SECOND);
		firstDateOfMonth.clear(Calendar.MINUTE);
		firstDateOfMonth.set(Calendar.HOUR_OF_DAY, 0);
		firstDateOfMonth.set(Calendar.YEAR, year);
		firstDateOfMonth.set(Calendar.MONTH,month);
		firstDateOfMonth.set(Calendar.DAY_OF_MONTH, 1);
		
		Calendar startdate=(Calendar) firstDateOfMonth.clone();
		startdate.add(Calendar.DAY_OF_MONTH, Calendar.SUNDAY-startdate.get(Calendar.DAY_OF_WEEK));
		
		Calendar lastDateOfMonth=(Calendar) firstDateOfMonth.clone();
		lastDateOfMonth.set(Calendar.DAY_OF_MONTH,firstDateOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH));
		
		Calendar lastDate=(Calendar) lastDateOfMonth.clone();
		lastDate.add(Calendar.DAY_OF_MONTH, Calendar.SATURDAY-lastDate.get(Calendar.DAY_OF_WEEK));
		
		while(!startdate.after(lastDate)){
			List<EventForView> events=null;
			if(startdate.before(firstDateOfMonth)){
				events=this.getEventsByDay(startdate, now, currentUser, false);
			}else if(startdate.after(lastDateOfMonth)){
				events=this.getEventsByDay(startdate, now, currentUser, false);
			}else{
				events=this.getEventsByDay(startdate, now, currentUser, true);
			}
			maps.put(startdate.getTimeInMillis(), events);
			startdate.add(Calendar.DAY_OF_MONTH, 1);
		}
		return maps;
	}
	
	
	public Map getEventsByWeek(int year, int numweek, Calendar now,
			User currentUser) {
		Map maps=new HashMap();
		Calendar firstDateOfWeek=Calendar.getInstance();
		firstDateOfWeek.clear(Calendar.MILLISECOND);
		firstDateOfWeek.clear(Calendar.SECOND);
		firstDateOfWeek.clear(Calendar.MINUTE);
		firstDateOfWeek.set(Calendar.HOUR_OF_DAY, 0);
		firstDateOfWeek.set(Calendar.YEAR, year);
		firstDateOfWeek.set(Calendar.WEEK_OF_YEAR,numweek);
		firstDateOfWeek.add(Calendar.DAY_OF_MONTH, Calendar.SUNDAY-firstDateOfWeek.get(Calendar.DAY_OF_WEEK));
		for(int i=0;i<7;i++){
			List<EventForView> events=null;
			if(firstDateOfWeek.get(Calendar.MONTH)==now.get(Calendar.MONTH)&&firstDateOfWeek.get(Calendar.YEAR)==firstDateOfWeek.get(Calendar.YEAR)){
				events=this.getEventsByDay(firstDateOfWeek, now, currentUser, true);
			}else{
				events=this.getEventsByDay(firstDateOfWeek, now, currentUser, false);
			}
			maps.put(firstDateOfWeek.getTimeInMillis(), events);
			firstDateOfWeek.add(Calendar.DAY_OF_MONTH, 1);
		}
		return maps;
	}
	
//	public List<ClipEvent> pagingClipEventByOptions(String option)	
//	private List<EventForView> filterEventForToday(List<DateEvent> dates2,
//			long todaybegin, long todayend, Calendar today,boolean isThisMonth) {
//		List<EventForView> list=new ArrayList<EventForView>();
//		for (DateEvent dateEvent : dates2) {
//			long datetime=dateEvent.getDatetime();
//			long clocktime=dateEvent.getAlertstatus();
//			String repeatid=dateEvent.getRepeatid();
//			if(repeatid.startsWith("month")){
//				int monthid=Integer.parseInt(repeatid.split("_")[1]);
//				MonthRepeat monthRepeat=monthRepeatDAO.findById(monthid);
//				if(datetime<todayend){
//					Calendar calStart=Calendar.getInstance();
//					calStart.setTimeInMillis(datetime);
//					if(calStart.getTimeInMillis()>=todaybegin){
//						EventForView eventForView=new EventForView();
//						eventForView.setThisMonth(isThisMonth);
//						eventForView.setEventid("date_"+dateEvent.getId());
//						eventForView.setAddress(dateEvent.getAddress());
//						eventForView.setContent(dateEvent.getContent());
//						eventForView.setTitle(dateEvent.getTitle());
//						eventForView.setAllDay(true);
//						eventForView.setStarthour(-1);
//						eventForView.setEndhour(-1);
//						if(clocktime==0l){
//							eventForView.setClocked(true);
//						}else{
//							if(clocktime<today.getTimeInMillis()){
//								eventForView.setClocked(true);
//							}else
//								eventForView.setClocked(false);
//						}
//						eventForView.setEventtype(dateEvent.getEventtype());
//						eventForView.setStarttime(calStart.getTimeInMillis());
//						eventForView.setEndtime(calStart.getTimeInMillis());
//						eventForView.setClocktime(date2String(clocktime));
//						eventForView.setRepeatdesc("每个月重复");
//						list.add(eventForView);
//					}
//					if(monthRepeat.getTimes()==-1){
//						while(true){
//							calStart.add(Calendar.MONTH, monthRepeat.getRepeatinterval());
//							if(calStart.getTimeInMillis()>=todayend){
//								break;
//							}else if(calStart.getTimeInMillis()>=todaybegin){
//								EventForView eventForView=new EventForView();
//								eventForView.setThisMonth(isThisMonth);
//								eventForView.setEventid("date_"+dateEvent.getId());
//								eventForView.setAddress(dateEvent.getAddress());
//								eventForView.setContent(dateEvent.getContent());
//								eventForView.setTitle(dateEvent.getTitle());
//								eventForView.setAllDay(true);
//								eventForView.setStarthour(-1);
//								eventForView.setEndhour(-1);
//								if(clocktime==0l){
//									eventForView.setClocked(true);
//								}else{
//									if(clocktime<today.getTimeInMillis()){
//										eventForView.setClocked(true);
//									}else
//										eventForView.setClocked(false);
//								}
//								eventForView.setEventtype(dateEvent.getEventtype());
//								eventForView.setStarttime(calStart.getTimeInMillis());
//								eventForView.setEndtime(calStart.getTimeInMillis());
//								eventForView.setClocktime(date2String(clocktime));
//								eventForView.setRepeatdesc("每个月重复");
//								list.add(eventForView);
//							}
//						}
//					}else if(monthRepeat.getTimes()!=0){
//						int retimes=monthRepeat.getTimes();
//						while(retimes>0){
//							calStart.add(Calendar.MONTH, monthRepeat.getRepeatinterval());
//							if(calStart.getTimeInMillis()>=todayend){
//								break;
//							}else if(calStart.getTimeInMillis()>=todaybegin){
//								EventForView eventForView=new EventForView();
//								eventForView.setThisMonth(isThisMonth);
//								eventForView.setEventid("date_"+dateEvent.getId());
//								eventForView.setAddress(dateEvent.getAddress());
//								eventForView.setContent(dateEvent.getContent());
//								eventForView.setTitle(dateEvent.getTitle());
//								eventForView.setAllDay(true);
//								eventForView.setStarthour(-1);
//								eventForView.setEndhour(-1);
//								if(clocktime==0l){
//									eventForView.setClocked(true);
//								}else{
//									if(clocktime<today.getTimeInMillis()){
//										eventForView.setClocked(true);
//									}else
//										eventForView.setClocked(false);
//								}
//								eventForView.setEventtype(dateEvent.getEventtype());
//								eventForView.setStarttime(calStart.getTimeInMillis());
//								eventForView.setEndtime(calStart.getTimeInMillis());
//								eventForView.setClocktime(date2String(clocktime));
//								eventForView.setRepeatdesc("每个月重复");
//								list.add(eventForView);
//							}
//							retimes--;
//						}
//					}else{
//						long endday=monthRepeat.getEndday();
//						while(true){
//							calStart.add(Calendar.MONTH, monthRepeat.getRepeatinterval());
//							if(calStart.getTimeInMillis()>=todayend){
//								break;
//							}else if(calStart.getTimeInMillis()>endday){//判断是否超过设定的终止时间
//								break;
//							}else if(calStart.getTimeInMillis()>=todaybegin){
//								EventForView eventForView=new EventForView();
//								eventForView.setThisMonth(isThisMonth);
//								eventForView.setEventid("date_"+dateEvent.getId());
//								eventForView.setAddress(dateEvent.getAddress());
//								eventForView.setContent(dateEvent.getContent());
//								eventForView.setTitle(dateEvent.getTitle());
//								eventForView.setAllDay(true);
//								eventForView.setStarthour(-1);
//								eventForView.setEndhour(-1);
//								if(clocktime==0l){
//									eventForView.setClocked(true);
//								}else{
//									if(clocktime<today.getTimeInMillis()){
//										eventForView.setClocked(true);
//									}else
//										eventForView.setClocked(false);
//								}
//								eventForView.setEventtype(dateEvent.getEventtype());
//								eventForView.setStarttime(calStart.getTimeInMillis());
//								eventForView.setEndtime(calStart.getTimeInMillis());
//								eventForView.setClocktime(date2String(clocktime));
//								eventForView.setRepeatdesc("每个月重复");
//								list.add(eventForView);
//							}
//						}
//					}
//				}
//			}else if(repeatid.startsWith("year")){
//				int yearid=Integer.parseInt(repeatid.split("_")[1]);
//				YearRepeat yearRepeat=yearRepeatDAO.findById(yearid);
//				if(datetime<todayend){
//					Calendar calStart=Calendar.getInstance();
//					calStart.setTimeInMillis(datetime);
//					if(calStart.getTimeInMillis()>=todaybegin){
//						EventForView eventForView=new EventForView();
//						eventForView.setThisMonth(isThisMonth);
//						eventForView.setEventid("date_"+dateEvent.getId());
//						eventForView.setAddress(dateEvent.getAddress());
//						eventForView.setContent(dateEvent.getContent());
//						eventForView.setTitle(dateEvent.getTitle());
//						eventForView.setAllDay(true);
//						eventForView.setStarthour(-1);
//						eventForView.setEndhour(-1);
//						if(clocktime==0l){
//							eventForView.setClocked(true);
//						}else{
//							
//							if(clocktime<today.getTimeInMillis()){
//								eventForView.setClocked(true);
//							}else
//								eventForView.setClocked(false);
//						}
//						eventForView.setEventtype(dateEvent.getEventtype());
//						eventForView.setStarttime(calStart.getTimeInMillis());
//						eventForView.setEndtime(calStart.getTimeInMillis());
//						eventForView.setClocktime(date2String(clocktime));
//						eventForView.setRepeatdesc("每年重复");
//						list.add(eventForView);
//					}
//					if(yearRepeat.getTimes()==-1){
//						while(true){
//							calStart.add(Calendar.YEAR, 1);
//							if(calStart.getTimeInMillis()>=todayend){
//								break;
//							}else if(calStart.getTimeInMillis()>=todaybegin){
//								EventForView eventForView=new EventForView();
//								eventForView.setThisMonth(isThisMonth);
//								eventForView.setEventid("date_"+dateEvent.getId());
//								eventForView.setAddress(dateEvent.getAddress());
//								eventForView.setContent(dateEvent.getContent());
//								eventForView.setTitle(dateEvent.getTitle());
//								eventForView.setAllDay(true);
//								eventForView.setStarthour(-1);
//								eventForView.setEndhour(-1);
//								if(clocktime==0l){
//									eventForView.setClocked(true);
//								}else{
//									
//									if(clocktime<today.getTimeInMillis()){
//										eventForView.setClocked(true);
//									}else
//										eventForView.setClocked(false);
//								}
//								eventForView.setEventtype(dateEvent.getEventtype());
//								eventForView.setStarttime(calStart.getTimeInMillis());
//								eventForView.setEndtime(calStart.getTimeInMillis());
//								eventForView.setClocktime(date2String(clocktime));
//								eventForView.setRepeatdesc("每年重复");
//								list.add(eventForView);
//							}
//						}
//					}else if(yearRepeat.getTimes()!=0){
//						int retimes=yearRepeat.getTimes();
//						while(retimes>0){
//							calStart.add(Calendar.YEAR, 1);
//							if(calStart.getTimeInMillis()>=todayend){
//								break;
//							}else if(calStart.getTimeInMillis()>=todaybegin){
//								EventForView eventForView=new EventForView();
//								eventForView.setThisMonth(isThisMonth);
//								eventForView.setEventid("date_"+dateEvent.getId());
//								eventForView.setAddress(dateEvent.getAddress());
//								eventForView.setContent(dateEvent.getContent());
//								eventForView.setTitle(dateEvent.getTitle());
//								eventForView.setAllDay(true);
//								eventForView.setStarthour(-1);
//								eventForView.setEndhour(-1);
//								if(clocktime==0l){
//									eventForView.setClocked(true);
//								}else{
//									
//									if(clocktime<today.getTimeInMillis()){
//										eventForView.setClocked(true);
//									}else
//										eventForView.setClocked(false);
//								}
//								eventForView.setEventtype(dateEvent.getEventtype());
//								eventForView.setStarttime(calStart.getTimeInMillis());
//								eventForView.setEndtime(calStart.getTimeInMillis());
//								eventForView.setClocktime(date2String(clocktime));
//								eventForView.setRepeatdesc("每年重复");
//								list.add(eventForView);
//							}
//							retimes--;
//						}
//					}else{
//						long endday=yearRepeat.getEndday();
//						while(true){
//							calStart.add(Calendar.YEAR, 1);
//							if(calStart.getTimeInMillis()>=todayend){
//								break;
//							}else if(calStart.getTimeInMillis()>endday){//判断是否超过设定的终止时间
//								break;
//							}else if(calStart.getTimeInMillis()>=todaybegin){
//								EventForView eventForView=new EventForView();
//								eventForView.setThisMonth(isThisMonth);
//								eventForView.setEventid("date_"+dateEvent.getId());
//								eventForView.setAddress(dateEvent.getAddress());
//								eventForView.setContent(dateEvent.getContent());
//								eventForView.setTitle(dateEvent.getTitle());
//								eventForView.setAllDay(true);
//								eventForView.setStarthour(-1);
//								eventForView.setEndhour(-1);
//								if(clocktime==0l){
//									eventForView.setClocked(true);
//								}else{
//									if(clocktime<today.getTimeInMillis()){
//										eventForView.setClocked(true);
//									}else
//										eventForView.setClocked(false);
//								}
//								eventForView.setEventtype(dateEvent.getEventtype());
//								eventForView.setStarttime(calStart.getTimeInMillis());
//								eventForView.setEndtime(calStart.getTimeInMillis());
//								eventForView.setClocktime(date2String(clocktime));
//								eventForView.setRepeatdesc("每年重复");
//								list.add(eventForView);
//							}
//						}
//					}
//				}
//			}
//		}
//		return list;
//	}

	
	
}
